package com.zzg.util;



import java.io.BufferedReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.Map;
import java.util.Set;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.zzg.entity.ShowMap;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @author zzg
 * @version 1.0.0
 * @ClassName：MapUtil
 * @Description: 地图IP获取工具类
 * @date 2021/7/20
 **/
public class MapUtil {
    static String AK = "o3jHuvpU25ypn5oSfYFnWTa4olTSlDWo"; // 百度地图密钥
    public static ShowMap getCoordinate(String address) {
        String ipX="";
        String ipY="";
        if (address != null && !"".equals(address)) {
            String url = "http://api.map.baidu.com/geocoding/v3/?address="+address+"=json&ak="+AK+"&callback=showLocation";
            String json = loadJSON(url);
            Map<String, String> map=new HashMap();
            Map<String, String> stringStringMap =MapUtil.parseXml2Map(json, map);
            Set<String> strings = stringStringMap.keySet();
            for (String string : strings) {
                String[] split = string.split("\\.");
                if (split[split.length-1] .equals("lat")) {
                    ipY=map.get(string);
                }else if (split[split.length-1] .equals("lng")) {
                    ipX=map.get(string);
                }
            }
        }
        ShowMap showMap=new ShowMap();
        showMap.setiPX(ipX);
        showMap.setiPY(ipY);
        return showMap;
    }

    private static String loadJSON(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {} catch (IOException e) {}
        return json.toString();
    }

    private static Map<String, String> parseXml2Map(String xml, Map<String, String> map) {
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(new StringReader(xml));
            Element root = doc.getRootElement();
            String path = "";
            if (map.containsKey(root.getName().trim())) {
                path = map.get(root.getName().trim());
                map.remove(root.getName().trim());
            }
            for (Iterator i = root.elementIterator(); i.hasNext();) {
                Element element = (Element) i.next();
                if (element.isTextOnly()) {
                    if (path.length() > 0) {
                        map.put(path + element.getName().trim(), element.getTextTrim());
                    } else {
                        map.put(element.getName().trim(), element.getTextTrim());
                    }
                } else {
                    map.put(element.getName().trim(), path+ element.getName().trim() + ".");
                    parseXml2Map(element.asXML(), map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
